package com.zoro.barn.business.service.system.business.role.service;

import com.zoro.barn.business.service.system.business.role.dao.BarnRoleMapper;
import com.zoro.barn.business.service.system.business.role.entity.BarnRole;
import com.zoro.barn.business.service.system.business.role.entity.BarnRoleMenuDto;
import com.zoro.barn.business.service.system.business.role.entity.BarnRoleUserDto;
import com.zoro.barn.business.service.system.business.rolemenu.dao.BarnRoleMenuMapper;
import com.zoro.barn.business.service.system.business.roleuser.dao.BarnRoleUserMapper;
import com.zoro.barn.business.service.system.business.teamrole.dao.BarnTeamRoleMapper;
import com.zoro.barn.commons.tools.string.IDs;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author zhaoxingwu
 */
@Service
public class BarnRoleService {

    @Resource
    private BarnRoleMapper barnRoleMapper;
    @Resource
    private BarnRoleMenuMapper barnRoleMenuMapper;
    @Resource
    private BarnRoleUserMapper barnRoleUserMapper;
    @Resource
    private BarnTeamRoleMapper barnTeamRoleMapper;


    public BarnRole getById(String pid) {
        return this.barnRoleMapper.getById(pid);
    }

    public List<BarnRole> query(BarnRole role) {
        return this.barnRoleMapper.query(role);
    }

    /**
     * 保存数据，insert or update,it's dependent on if there has pid
     *
     * @param data data
     * @return data
     */
    @Transactional(rollbackFor = Exception.class)
    public BarnRole save(BarnRole data) {
        if (StringUtils.isBlank(data.getPid())) {
            data.setPid(IDs.uuid());
            data.setCreateTime(new Date());
            this.barnRoleMapper.insert(data);
        } else {
            data.setUpdateTime(new Date());
            this.barnRoleMapper.update(data);
        }
        return data;
    }

    /**
     * 主键删除
     *
     * @param pid 主键
     */
    @Transactional(rollbackFor = Exception.class)
    public void delete(String pid) {
        this.barnRoleMapper.deleteById(pid);
        this.barnRoleMenuMapper.deleteByRole(pid);
        this.barnRoleUserMapper.deleteByRole(pid);
        this.barnTeamRoleMapper.deleteByRole(pid);
    }

    /**
     * 查找用户的所有角色
     *
     * @param userId userId
     * @return 角色
     */
    public List<BarnRole> getByUserId(String userId) {
        return this.barnRoleMapper.getByUserId(userId);
    }

    public List<BarnRole> getByUserTeam(Set<String> teamIds) {
        if (CollectionUtils.isEmpty(teamIds)) {
            return null;
        }

        return this.barnRoleMapper.getByTeamIds(teamIds);



    }

    /**
     * 查询某个角色的用户情况
     *
     * @param dto dto
     * @return result
     */
    public List<BarnRoleUserDto> getUsers(BarnRoleUserDto dto) {
        if (StringUtils.isBlank(dto.getRoleId())) {
            return null;
        }
        return this.barnRoleMapper.getUsers(dto);
    }


    /**
     * find menus that one role have
     *
     * @param dto dto
     * @return result
     */
    public List<BarnRoleMenuDto> getMenus(BarnRoleMenuDto dto) {
        return this.barnRoleMapper.getMenus(dto);
    }

    /**
     * find one team's default roles
     *
     * @param teamId team
     * @return result set
     */
    public List<BarnRole> getByTeamId(String teamId) {
        return this.barnRoleMapper.getByTeamId(teamId);
    }

}
